﻿using Dapper;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class Resource_DAL
    {
        /// <summary>
        /// 获取Resource对象的集合
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Resource> SearchResourceList()
        {
            using (IDbConnection conn = Hepler.MySqlOpenConnection())
            {
                const string query = "select * from System_Content_Basic_Resource order by CreateTime desc";
                
                
                return SqlMapper.Query<Resource>(conn, query);
            }
        }

        public bool Add(Resource resource)
        {
            using (IDbConnection conn = Hepler.MySqlOpenConnection())
            {
                System.Text.StringBuilder sqlBuilder = new System.Text.StringBuilder(512);
                sqlBuilder.Append("INSERT INTO System_Content_Basic_Resource");
                
                System.Text.StringBuilder sqlIntoBuilder = new System.Text.StringBuilder(216);
                System.Text.StringBuilder sqlValueBuilder = new System.Text.StringBuilder(216);

                //System.Collections.ObjectModel.Collection<System.Data.IDataParameter> sqlParameters = new System.Collections.ObjectModel.Collection<System.Data.IDataParameter>();

                
                if (resource.MIME == null)
                {
                    throw new System.Exception("resource.MIME不能为空");
                }
                sqlIntoBuilder.Append("MIME");
                //sqlValueBuilder.Append("@MIME");
                sqlValueBuilder.Append("'"+resource.MIME+"'");
                //sqlParameters.Add(new SqlParameter("@MIME", resource.MIME));

                
                if (resource.Path == null)
                {
                    throw new System.Exception("resource.Path不能为空");
                }
                sqlIntoBuilder.Append(", Path");
                sqlValueBuilder.Append(",'" + resource.Path.Replace("\\","\\\\")+ "'");
                //sqlValueBuilder.Append(", @Path");
                //sqlParameters.Add(new SqlParameter("@Path", resource.Path));

                
                if (resource.Property == null)
                {
                    throw new System.Exception("resource.Property不能为空");
                }
                sqlIntoBuilder.Append(", Property");
                sqlValueBuilder.Append(",'"+resource.Property+"'");
                //sqlValueBuilder.Append(", @Property");
                //sqlParameters.Add(new SqlParameter("@Property", resource.Property));

                
                if (resource.CreateTime == null)
                {
                    throw new System.Exception("resource.CreateTime不能为空");
                }
                sqlIntoBuilder.Append(", CreateTime");
                sqlValueBuilder.Append(",'"+resource.CreateTime+"'");
                //sqlValueBuilder.Append(", @CreateTime");
                //sqlParameters.Add(new SqlParameter("@CreateTime", resource.CreateTime));

                
                if (resource.LastUpdateTime == null)
                {
                    throw new System.Exception("resource.LastUpdateTime不能为空");
                }
                sqlIntoBuilder.Append(", LastUpdateTime");
                sqlValueBuilder.Append(",'" + resource.LastUpdateTime + "'");
                //sqlValueBuilder.Append(", @LastUpdateTime");
                //sqlParameters.Add(new SqlParameter("@LastUpdateTime", resource.LastUpdateTime));

                sqlIntoBuilder.Append(",Size");
                sqlValueBuilder.Append("," + resource.Size);
                //sqlValueBuilder.Append(", @Size");
                //sqlParameters.Add(new SqlParameter("@Size", resource.Size));

                if (resource.Name == null)
                {
                    throw new System.Exception("resource.Name不能为空");
                }
                sqlIntoBuilder.Append(",Name");
                sqlValueBuilder.Append(",'" + resource.Name+"'");
                //sqlValueBuilder.Append(",@Name");
                //sqlParameters.Add(new SqlParameter("@Name", resource.Name));

                sqlBuilder.Append(" ( ");
                sqlBuilder.Append(sqlIntoBuilder);
                sqlBuilder.Append(" ) ");
                sqlBuilder.Append(" VALUES ");
                sqlBuilder.Append(" ( ");
                sqlBuilder.Append(sqlValueBuilder);
                sqlBuilder.Append(" ) ");
                string sql = sqlBuilder.ToString();
                //CommandDefinition d = new CommandDefinition(sqlBuilder.ToString(), sqlParameters.ToArray());
                //return SqlMapper.Execute(conn, d) > 0;
                return SqlMapper.Execute(conn, sqlBuilder.ToString()) > 0;
            }
        }

        public Int64 Add2(Resource resource)
        {
            bool result = Add(resource);
            if (result)
            {
                Resource model = GetByCreateTime(resource.CreateTime);
                if (model != null)
                {
                    return model.ResourceId;
                }
            }
            return 0;
        }

        public Resource GetById(Int64 id)
        {
            using (IDbConnection conn = Hepler.MySqlOpenConnection())
            {
                string query = "select * from System_Content_Basic_Resource where ResourceId=" + id;
                return SqlMapper.Query<Resource>(conn, query).FirstOrDefault();
            }
        }

        public Resource GetByCreateTime(DateTime createTime)
        {
            using (IDbConnection conn = Hepler.MySqlOpenConnection())
            {
                string query = "select * from System_Content_Basic_Resource where CreateTime='" + createTime + "'";
                return SqlMapper.Query<Resource>(conn, query).FirstOrDefault();
            }
        }
    }
}
